package _110BalancedBinaryTree;

public class CSolution {

  public static void main(String[] args) {
    System.out.println("aa");
    TreeNode t0 = new TreeNode(3);
    TreeNode t1 = new TreeNode(4);
    TreeNode t2 = new TreeNode(20);
    TreeNode t3 = new TreeNode(15);
    TreeNode t4 = new TreeNode(7);
    TreeNode t5 = new TreeNode(8);
    t0.left=t1;
    t0.right = t2;
    t2.left = t3;
    t2.right = t4;
    t4.left = t5;
    System.out.println(process(t0));
  }

  private static boolean process(TreeNode node) {
    if (node == null) {
      return true;
    }
    int level = 1;
    int[] levels = new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE};
    getLevel(node, level, levels);
      return levels[1] - levels[0] <= 1;
  }

  private static void getLevel(TreeNode treeNode, int level, int[] levels) {
    if (treeNode == null) {
      processLevels(level, levels);
      return;
    }
    getLevel(treeNode.left, ++level, levels);
    getLevel(treeNode.right, level, levels);
  }

  private static void processLevels(int level, int[] levels) {
    if (levels[0] > level) {
      levels[0] = level;
    }
    if (levels[1] < level) {
      levels[1] = level;
    }
  }
}
